package cn.zkw.controller.back;

import cn.zkw.service.NoticeService;
import cn.zkw.service.WordsService;
import cn.zkw.vo.Notice;
import cn.zkw.vo.Words;
import com.google.gson.JsonObject;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileReader;
import java.io.Reader;
import java.util.Date;

@Controller
public class NoticeController {
    @Autowired
    NoticeService noticeService;

    @Autowired
    WordsService wordsService;

    /**
     * 添加公告
     * @param notice
     * @return
     */
    @RequestMapping("/back/notice/add")
    public ModelAndView addNotice(Notice notice, HttpServletRequest request){
        ModelAndView modelAndView = new ModelAndView("redirect:/back/addNotice");
        notice.setNotice_date(new Date());
        if(noticeService.addNotice(notice)>0){  //执行添加
            request.getSession().setMaxInactiveInterval(3);
            request.getSession().setAttribute("message","发布成功");
        }else{
            request.getSession().setMaxInactiveInterval(3);
            request.getSession().setAttribute("message","请求失败");
        }

        return modelAndView;
    }

    /**
     * 公告修改
     * @param request
     * @param notice
     * @return
     */
    @RequestMapping("/back/notice/update")
    public String updateNotice(HttpServletRequest request,Notice notice){
        if(noticeService.updateNotice(notice)>0){  //执行修改
            request.getSession().setMaxInactiveInterval(3);
            request.getSession().setAttribute("message","修改成功");
        }else{
            request.getSession().setMaxInactiveInterval(3);
            request.getSession().setAttribute("message","修改失败");
        }
        request.setAttribute("notice",notice);
        return "/back/update_notice";
    }

    /**
     * 公告删除
     * @param request
     * @param notice_id
     * @return
     */
    @RequestMapping(value = "/back/notice/delete",method = RequestMethod.GET,produces = "application/json")
    public @ResponseBody Object deleteNotice(HttpServletRequest request, Integer notice_id){
        System.out.println("************"+notice_id);
        JSONObject jsonObject = new JSONObject();
        if(noticeService.deleteNotice(notice_id)>0){  //执行修改
            request.getSession().setMaxInactiveInterval(3);
            request.getSession().setAttribute("message","删除成功");
            jsonObject.put("url","/back/notice");
        }else{
            request.getSession().setMaxInactiveInterval(3);
            request.getSession().setAttribute("message","删除失败");
            jsonObject.put("url","/back/notice");
        }
        return jsonObject;
    }

    /**
     * 添加每日一句
     * @param words
     * @return
     */
    @RequestMapping("/back/notice/words/add")
    public @ResponseBody Object addWords(Words words,HttpServletRequest request){
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("url","/back/notice");
        if(wordsService.addWords(words)>0){
            jsonObject.put("message","修改成功");
        }else{
            jsonObject.put("message","修改失败");
        }
        return jsonObject;
    }

}
